<?php

namespace app\admin\model\repair;

use app\admin\model\BaseMode;
use app\admin\model\Category;
use app\admin\model\Staff;
use think\Model;
use think\exception\ValidateException;
class Repair extends BaseMode
{


    protected $json=['expend_voucher','staff_json','thubms','staff_id'];

    protected $jsonAssoc = true;



    public function getStatusAttr($value)
    {
        $status=config('view.Goods_status');

        return $status[$value];
    }

    public function getExpendVoucherAttr($value)
    {


        return implode(',',$value);
    }
    public function getThubmsAttr($value)
    {
        return implode(',',$value);
    }


    public function getStaffIdAttr($value)
    {
        $staff=Staff::whereIn('id',$value)->column('name');

        return implode(',',$staff);
    }


    public function getCategoryInfo(){

        return $this->hasOne(Category::class,'id','cate_id')->bind(['cate_name'=>'name']);

    }
    public static function getList(){


        $where=[];
        $where[]=['store_id','=',session('store_id')];
        return (new Repair)->with(['getCategoryInfo'])->where($where)->page(input('page',1))->limit(input('limit',20))->select();
    }


    public static function insertRepair($input){
        try {
              $input['store_id']=session('store_id');
                self::create($input);
            return [
                'code' => 200,
                'msg' => '成功!',
                'url'=>url('/Repair/repairList')->build()
            ];
                // dump($result);die;
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $e->getError();
        }

    }


    public static function updateRepair($input){
        try {


            $input['store_id']=session('store_id');
            self::where(['id'=>$input['id']])->save($input);
            return [
                'code' => 200,
                'msg' => '成功!',
                'url'=>url('/Repair/repairList')->build()
            ];
            // dump($result);die;
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $e->getError();
        }

    }
}